﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Windows;
using DataProcessing;
namespace Gene.Render
{

    /// <summary>
    /// 
    /// </summary>
    public class CoordinateDetector
    {
        /// <summary>
        /// 根据用户点击的点。来计算映射到实际值域的点
        /// </summary>
        /// 
        public const int LEFT = -1;
        public const int RIGHT = 1;
        public int X_ZERO_POINT = -1;

        public const double ZONE = ChromePairData.RESOLUTION/ (RIGHT - LEFT);
        public String SelectedChrome
        {
            get
            {
                
                return Settings.Instance.ChromeSettingList[chrIndex].DisplayName;
            }
            
        }
        public long SelectedValue;
        public int chrIndex;
        Point3D prePoint;
        ValueRange[] currentWindow;

        public CoordinateDetector(Point3D point, ValueRange[] window)
        {
            X_ZERO_POINT = Scene.X_ZERO_POINT;
            prePoint = point;
            currentWindow = window;
        }
        public CoordinateDetector(ValueRange [] window)
        {
            currentWindow = window;
        }

        /// <summary>
        /// 
        /// </summary>
        public void GetMouseInfo()
        {

            double zzz = prePoint.Z;

            chrIndex = Scene.PlaneOfZ(zzz);
            uint pointPerPixel = PointPerPixel(currentWindow[chrIndex]);

            if (prePoint.X - X_ZERO_POINT <= 0)
            {
                SelectedValue = 0;
            }
            else
            {
                uint xstart = ((uint)((prePoint.X - X_ZERO_POINT) * ZONE)) * pointPerPixel + currentWindow[chrIndex].Start;
                uint xend = xstart + pointPerPixel;
                //data not loaded yet
                if (ChromePairData.Data==null)
                {
                    SelectedValue = 0;
                }
                else
                {
                    SelectedValue = ChromePairData.Data[chrIndex].GetActualPoint((xstart + xend) / 2) ;
                }
            }

        }


        private uint PointPerPixel(ValueRange obWindow)
        {
            return (obWindow.Size) / (uint)ChromePairData.RESOLUTION;
        }


        /// <summary>
        /// 注意，这里的10是"基本像素"，而不是真实像素
        /// </summary>
        const int ZOOMIN_WINDOW_RADIUS = 10;
        public ValueRange GetRange(double x, ValueRange currentWindow)
        {
            //取鼠标位置附近20个像素点pixel
            int leftpixelnumber = 0;
            int rightpixelnumber = 0;
            ValueRange ret = new ValueRange();
            
           
            //落入哪个基本像素
            uint pointPerPixel = PointPerPixel(currentWindow);

            int number = (int)((x-X_ZERO_POINT) * ZONE);

            leftpixelnumber = Math.Max(0, number - ZOOMIN_WINDOW_RADIUS);
            leftpixelnumber = Math.Min(ChromePairData.RESOLUTION, leftpixelnumber);


            rightpixelnumber =Math.Min(ChromePairData.RESOLUTION, number + ZOOMIN_WINDOW_RADIUS);
            rightpixelnumber = Math.Max(0, rightpixelnumber);


            ret.Start =(uint)Math.Min(leftpixelnumber * pointPerPixel + currentWindow.Start,currentWindow.End);
            ret.End = (uint)Math.Min(rightpixelnumber * pointPerPixel + currentWindow.Start, currentWindow.End);

            return ret;
        }

    }
}
